//
//  NMPOScanResultViewController.swift
//  NMPO
//
//  Created by PowerfullClean on 2023/10/31.
//

import UIKit

class NMPOScanResultViewController: NMPOResultViewController {
    init(nmpo_funcType: NMPOFuncType = .nmpo_speed, nmpo_resultModel: Any? = nil) {
        super.init(nibName: nil, bundle: nil)
        self.nmpo_funcType = nmpo_funcType
        self.nmpo_resultModel = nmpo_resultModel
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        NMPOManager.nmpo_manager.nmpo_delegate = nil
        NMPOManager.nmpo_manager.nmpo_stopSpeedScan()
        
        nmpo_navView.nmpo_titleLabel.text = nmpo_funcType.nmpo_title
        nmpo_funcModels = nmpo_funcModels.filter({ nmpo_model in
            return nmpo_model.nmpo_type != nmpo_funcType
        })
    }
    // MARK: - nmpo action
    override func nmpo_collectionViews() {
        super.nmpo_collectionViews()
        view.addSubview(nmpo_navView)
        nmpo_collectionView.snp.makeConstraints { make in
            make.bottom.left.width.equalToSuperview()
            make.top.equalTo(nmpo_navView.snp.bottom)
        }
        nmpo_collectionView.register(NMPOIPResultInfoCell.classForCoder(), forCellWithReuseIdentifier: NMPOIPResultInfoCell.nmpo_viewToString)
        nmpo_collectionView.register(NMPOSpeedResultInfoCell.classForCoder(), forCellWithReuseIdentifier: NMPOSpeedResultInfoCell.nmpo_viewToString)
        nmpo_collectionView.register(NMPOScanResultItemCell.classForCoder(), forCellWithReuseIdentifier: NMPOScanResultItemCell.nmpo_viewToString)
        nmpo_collectionView.dataSource = self
        nmpo_collectionView.delegate = self
    }
    // MARK: - nmpo lazy
    var nmpo_funcType: NMPOFuncType = .nmpo_speed
    var nmpo_resultModel: Any?
    override func nmpo_baseBgName() -> String? {
        return "nmpo_result_testBg"
    }
    lazy var nmpo_navView: NMPONavView = {
        let nmpo_navView = NMPONavView()
        nmpo_navView.nmpo_leftBlock = {[weak self] in
            self?.navigationController?.popToRootViewController(animated: true)
        }
        return nmpo_navView
    }()
    lazy var nmpo_funcModels: [NMPOFuncModel] = {
        var nmpo_funcModels = [NMPOFuncModel]()
        nmpo_funcModels.append(NMPOFuncModel(nmpo_type: .nmpo_connect,nmpo_title: "Better node location", nmpo_icon: "nmpo_result_testBetter"))
        nmpo_funcModels.append(NMPOFuncModel(nmpo_type: .nmpo_speed,nmpo_title: "Speed test", nmpo_icon: "nmpo_result_testSpeed2"))
        nmpo_funcModels.append(NMPOFuncModel(nmpo_type: .nmpo_ip,nmpo_title: "IP test", nmpo_icon: "nmpo_result_testIp2"))
        nmpo_funcModels.append(NMPOFuncModel(nmpo_type: .nmpo_device, nmpo_title: "Device infor" ,nmpo_icon: "nmpo_func_device"))
        return nmpo_funcModels
    }()
}

extension NMPOScanResultViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 1 {
            return nmpo_funcModels.count
        }
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 1 {
            let nmpo_cell = collectionView.dequeueReusableCell(withReuseIdentifier: NMPOScanResultItemCell.nmpo_viewToString, for: indexPath) as! NMPOScanResultItemCell
            nmpo_cell.nmpo_funcModel = nmpo_funcModels[indexPath.row]
            return nmpo_cell
        }
        if nmpo_funcType == .nmpo_ip {
            let nmpo_cell = collectionView.dequeueReusableCell(withReuseIdentifier: NMPOIPResultInfoCell.nmpo_viewToString, for: indexPath) as! NMPOIPResultInfoCell
            nmpo_cell.nmpo_ipScanModel = (nmpo_resultModel as? NMPOIPScanModel)
            return nmpo_cell
        }
        if nmpo_funcType == .nmpo_device {
            let nmpo_cell = collectionView.dequeueReusableCell(withReuseIdentifier: NMPODeviceResultCell.nmpo_viewToString, for: indexPath) as! NMPODeviceResultCell
            nmpo_cell.nmpo_deviceModels = nmpo_resultModel as? [NMPODeviceModel]
            return nmpo_cell
        }
        let nmpo_cell = collectionView.dequeueReusableCell(withReuseIdentifier: NMPOSpeedResultInfoCell.nmpo_viewToString, for: indexPath) as! NMPOSpeedResultInfoCell
        nmpo_cell.nmpo_speedValueModel = (nmpo_resultModel as? NMPOSpeedValueModel)
        return nmpo_cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 1 {
            return CGSize(width: nmpo_SCREEN_WIDTH-40, height: 86)
        }
        return CGSize(width: nmpo_SCREEN_WIDTH-40, height: 172)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 25, right: 0)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            return
        }
        let nmpo_funcModel = nmpo_funcModels[indexPath.row]
        if nmpo_funcModel.nmpo_type == .nmpo_connect {
            NMPOManager.nmpo_manager.nmpo_connectSmartAction()
            navigationController?.popToRootViewController(animated: true)
            return
        }
        navigationController?.pushViewController(NMPOScanViewController(nmpo_funcType: nmpo_funcModel.nmpo_type), animated: true)
    }
}
